package com.cskaoyan.wordmemorize.controller.app;

import com.cskaoyan.wordmemorize.auth.SaUserCheckPermission;
import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.UserVocAISituationDTO;
import com.cskaoyan.wordmemorize.dto.app.AIReviewReadDTO;
import com.cskaoyan.wordmemorize.dto.app.DisplayUserVocSentenceDTO;
import com.cskaoyan.wordmemorize.dto.app.UserFavoriteDTO;
import com.cskaoyan.wordmemorize.request.AIReviewReadCommand;
import com.cskaoyan.wordmemorize.dto.app.UserVocBookStatisticsDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserFavoriteCommand;
import com.cskaoyan.wordmemorize.request.UserVocExampleSentenceCommand;
import com.cskaoyan.wordmemorize.service.UserPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user/priviledge")
public class AppUserPrivilegeController {

    @Autowired
    UserPrivilegeService userPrivilegeService;


    // todo APP端单个添加收藏单词
    @PostMapping("favorite")
    public Result addFavorite(@RequestBody UserFavoriteCommand userFavoriteCommand) {

        // 用户ID要自己获取
        Long userId = StpKit.USER.getLoginIdAsLong();

        // 把所需数据传输过去
        userPrivilegeService.addFavorite(userFavoriteCommand, userId);

        return Result.ok();
    }


    // todo APP端批量添加收藏单词
    @PostMapping("favorite/bulk")
    public Result addFavoriteBulk(@RequestBody List<UserFavoriteCommand> userFavoriteCommand) {

        // 用户ID要自己获取
        Long userId = StpKit.USER.getLoginIdAsLong();

        userPrivilegeService.addFavoriteBulk(userFavoriteCommand, userId);

        return Result.ok();
    }

    // todo 取消收藏
    @PostMapping("favorite/cancel")
    public Result cancelFavorite(@RequestBody String[] array) {

        // 用户ID要自己获取
        Long userId = StpKit.USER.getLoginIdAsLong();

        // 传过来的是一个字符串数组String[]，service那边需要List<Long>

        // 将 String[] 转换为 List<Long>
        List<Long> longList = Arrays.stream(array)
                .map(Long::parseLong) // 将每个 String 转换为 Long
                .collect(Collectors.toList()); // 收集为 List<Long>

        // 把所需数据传输过去
        userPrivilegeService.cancelFavorite(longList, userId);

        return Result.ok();
    }


    // todo APP端获取单词收藏列表--根据关键字搜索
    @GetMapping("favorite/list")
    public Result<PageDTO<UserFavoriteDTO>> getUserFavoriteList(PageRequest pageRequest) {

        // 用户ID要自己获取
        Long userId = StpKit.USER.getLoginIdAsLong();

        // 把所需数据传输过去，并接受返回值
        PageDTO<UserFavoriteDTO> pageDTO = userPrivilegeService.getUserFavoriteList(pageRequest, userId);

        return Result.ok(pageDTO);
    }


    // todo APP端添加用户自建例句
    @PostMapping("voc/example")
    public Result addUserVocSentence(@RequestBody UserVocExampleSentenceCommand userVocExampleSentenceCommand) {

        // 用户ID要自己获取
        Long userId = StpKit.USER.getLoginIdAsLong();

        // 把所需数据传输过去
        userPrivilegeService.addUserVocSentence(userVocExampleSentenceCommand, userId);

        return Result.ok();
    }

    // todo：APP端修改用户自建例句(syl)
    @PutMapping("voc/example")
    public Result updateUserVocSentence(@RequestBody UserVocExampleSentenceCommand command) {
        // 用户ID要自己获取
        Long userId = StpKit.USER.getLoginIdAsLong();

        userPrivilegeService.updateUserVocSentence(command, userId);
        return Result.ok();
    }

    // todo APP端获取用户自建例句
    @GetMapping("voc/example/list")
    public Result<PageDTO<DisplayUserVocSentenceDTO>> getUserVocSentence(PageRequest pageRequest){
        // 用户ID要自己获取
        Long userId = StpKit.USER.getLoginIdAsLong();
        // 把所需数据传输过去，并接受返回值
        PageDTO<DisplayUserVocSentenceDTO> pageDTO = userPrivilegeService.getUserVocSentence(pageRequest, userId);
        return Result.ok(pageDTO);
    }

    // todo：APP端标识用户不想背（syl）
    @PostMapping("/voc/non")
    public Result notRemember(Long vocId){
        // 用户ID要自己获取
       Long userId = StpKit.USER.getLoginIdAsLong();
       // Long userId = 1926869600184836097L;

        userPrivilegeService.notRemember(vocId,userId);
        return Result.ok();
    }

    // todo:APP端取消用户不想背(syl)
    @PutMapping("/voc/non/cancel")
    public Result cancelNotRemember(Long vocId) {
        // 用户ID要自己获取
        Long userId = StpKit.USER.getLoginIdAsLong();
        // Long userId = 1926869600184836097L;

        userPrivilegeService.cancelNotRemember(vocId, userId);
        return Result.ok();
    }


    // todo AI阅读
    @PostMapping("review/ai/read")
    public Result<List<AIReviewReadDTO>> getAIReviewReadContent(@RequestBody AIReviewReadCommand command) {

        Long userId = StpKit.USER.getLoginIdAsLong();
        List<AIReviewReadDTO> list = userPrivilegeService.getAIReviewReadContent(command,userId);
        return Result.ok(list);
    }


    // todo APP端获取词书进度
    @SaUserCheckPermission("current:book")
    @GetMapping("current/{bookId}/statistics")
    public Result<UserVocBookStatisticsDTO> getSelectedBookStatistics(@PathVariable(value = "bookId") Long bookId) {

        // 用户ID要自己获取
        Long userId = StpKit.USER.getLoginIdAsLong();

        // 把所需数据传输过去，并接受返回值
        UserVocBookStatisticsDTO userVocBookStatisticsDTO = userPrivilegeService.getSelectedBookStatistics(userId, bookId);

        return Result.ok(userVocBookStatisticsDTO);

    }

    // todo APP端获取用户剩余免费AI阅读次数
    @PostMapping("review/ai/read/free")
    public Result<Integer> getUserVocAIReadRemainFreeCount(){
        // 获取正在访问的用户Id
        Long userId = StpKit.USER.getLoginIdAsLong();

        Integer freeCount = userPrivilegeService.getUserVocAIReadRemainFreeCount(userId);
        return Result.ok(freeCount);
    }

    // todo APP端获取AI阅读模式以及选项
    @PostMapping("review/ai/read/options")
    public Result<List<UserVocAISituationDTO>> getAIReviewReadOptions(){
        List<UserVocAISituationDTO> aiReviewReadOptions = userPrivilegeService.getAIReviewReadOptions();
        return Result.ok(aiReviewReadOptions);
    }

}
